﻿using Se.Log;
using Se.Model.Generator;
using SeClient.Module;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace SeClient.UnitTest
{
    public static class TestModule
    {
        /// <summary>
        /// Socket性能测试
        /// </summary>
        public static void TestSocketPerformance()
        {
            List<WorldNet> successClients = new List<WorldNet>();
            List<WorldNet> failedClients = new List<WorldNet>();
            for (var i = 20000; i < 30000; i++)
            {
                try
                {
                    WorldNet client = new WorldNet("127.0.0.1", i);
                    if (!client.TryConnect("127.0.0.1", 9901))
                    {
                        failedClients.Add(client);
                        continue;
                    }
                    Thread.Sleep(10);
                    SeLog.Info("发送{0}", i.ToString());
                    Thread.Sleep(10);
                    var bytes = PC_Send.Chat(1, i.ToString());
                    client.SendPackage(bytes);
                    successClients.Add(client);

                    if (i % 10 == 0)
                    {
                        SeLog.Info("Success Client {0}", successClients.Count);
                    }
                }
                catch (Exception ex)
                {
                    SeLog.Info("Error {0}", ex.ToString());
                }
            }
        }

        /// <summary>
        /// 开始单元测试
        /// </summary>
        public static void Begin(int number)
        {
            RegisterAccount(number, 10, "test");
        }

        /// <summary>
        /// 账户测试（timeout单位是ms）
        /// </summary>
        /// <param name="number"></param>
        /// <param name="timeout"></param>
        /// <param name="namePrefix">注册名前缀</param>
        public static void RegisterAccount(int number, int interval, string namePrefix = "")
        {
            for (var i = 0; i < number; i++)
            {
                CharacterModule.RegisterAccount(namePrefix + i, i.ToString());
                Thread.Sleep(interval);
            }
        }
    }
}
